package com.ywl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywl.pojo.Clazz;
import com.ywl.pojo.Student;
import com.ywl.service.ClazzService;
import com.ywl.service.StudentService;
import com.ywl.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author DL5O
 * @version 1.0
 */
@Api(tags = "班级控制器")
@RestController//   不是@Controller，异步Controller
@RequestMapping("/sms/clazzController")
public class ClazzController {
    @Autowired
    ClazzService clazzService;

    // /sms/clazzController/getClazzs
    @ApiOperation("查询所有班级信息")
    @GetMapping("/getClazzs")
    public Result getClazzs(){
        List<Clazz> clazzes = clazzService.getClazzs();
        return Result.ok(clazzes);
    }

    @ApiOperation("删除年级信息")
    @DeleteMapping("/deleteClazz")
    public Result deleteClazz(
            @RequestBody @ApiParam("年级信息") List<Integer> ids
    ) {
        clazzService.removeByIds(ids);
        return Result.ok();
    }

    // /sms/clazzController/getClazzsByOpr/1/3?gradeName=一年级&name=1
    @ApiOperation("分页带条件查询，查询班级信息")
    @GetMapping("/getClazzsByOpr/{pageNo}/{pageSize}")
    public Result getClazzsByOpr(
            @ApiParam("页码数") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("页大小") @PathVariable("pageSize") Integer pageSize,
            @ApiParam("查询条件") Clazz clazz
    ) {
        // 分页 带条件查询
        Page<Clazz> page = new Page<>(pageNo, pageSize);
        IPage<Clazz> pageRs = clazzService.getClazzOpr(page, clazz);
        return Result.ok(pageRs);
    }


    /**
     * 添加或者更新班级信息
     * @param clazz
     * @return
     */
    @ApiParam("添加或者更新班级信息")
    @PostMapping("/saveOrUpdateClazz")
    public Result saveOrUpdateClazz(
            @ApiParam("JSON格式的Grade对象") @RequestBody Clazz clazz
    )
    {
        clazzService.saveOrUpdate(clazz);
        return Result.ok();
    }
}
